package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.Response;
import com.example.demo.pojo.UserPoJo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author T480
 * 关注表业务实现
 */
@Service
public class UserServiceImpl implements UserService {

                @Autowired
                private UserMapper userMapper;

    /**
     * 用户登陆
     * @param userPoJo
     * @return
     */
    @Override
    public Response loging(UserPoJo userPoJo) {
        try {
            UserPoJo poJo = userMapper.selectById(userPoJo.getId());

            String encryptString = encryptString(userPoJo.getPassword());
            if (encryptString.equals(poJo.getPassword())){
                return Response.success();
            }
        }catch (Exception e){
            return  Response.error(500,"登陆失败！");
        }
        return Response.error(500,"登陆失败！");
    }

    /**
     * 用户注册更新
     * @param userPoJo
     * @return
     */
    @Override
    public Response inserOrUpdateUser(UserPoJo userPoJo) {
        try {
            if (userPoJo.getId()!=null){
                userMapper.updateById(userPoJo);
            }else {
                userMapper.insert(userPoJo);
            }
        }catch (Exception e){
            return  Response.error(500,"操作失败！");
        }
            return  Response.success();
    }
    /**
     * 用户删除
     * @param userPoJo
     * @return
     */
    @Override
    public Response deleteById(UserPoJo userPoJo) {
        try {
            if (userPoJo.getId()!=null){
                userPoJo.setUserStatus(1);
                userMapper.updateById(userPoJo);
            }
        }catch (Exception e){
            return  Response.error(500,"操作失败！");
        }
        return  Response.success();
    }
    /**
     * 用户列表分页查询
     * @param userPoJo
     * @return
     */
    @Override
    public Response userListPage(UserPoJo userPoJo) {
        try {
            Page page = new Page();
            page.setSize(userPoJo.getSize());
            page.setPages(userPoJo.getPage());

            userMapper.selectPage(page,null);

        }catch (Exception e){
            return  Response.error(500,"操作失败！");
        }
        return  Response.success();
    }


    /**
     * 使用MD5方式加密密码
     * @param input
     * @return
     */
    public static String encryptString(String input) {
        try {
            // 创建MD5加密算法实例
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 将输入字符串转换为字节数组
            byte[] inputBytes = input.getBytes();

            // 对字节数组进行加密
            byte[] hashedBytes = md.digest(inputBytes);

            // 将加密后的字节数组转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : hashedBytes) {
                sb.append(String.format("%02x", b));
            }

            // 返回加密后的字符串
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }
}
